#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
FROM registry.openanolis.cn/openanolis/anolisos:8.8

ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en'

RUN yum install -y tzdata openssl curl ca-certificates fontconfig gzip tar \
    && yum clean all

ENV EXTENDED_JAVA_VERSION 8.19.20

ENV STANDARD_JAVA_VERSION 8.19.20

COPY slim /usr/local/bin/slim

ARG EDITION=extended

ARG SOURCE=github

RUN set -eux; \
    ARCH="$(objdump="$(command -v objdump)" && objdump --file-headers "$objdump" | awk -F '[:,]+[[:space:]]+' '$1 == "architecture" { print $2 }')"; \
    case "${ARCH}" in \
       aarch64|arm64) \
         if [ "${SOURCE}" = "github" ];then \
           if [ "${EDITION}" = "extended" ];then \
             ESUM='317e6f18541c03cca96edd8c49d98450b11d2f36318495544a25e4fe0daa187b'; \
             BINARY_URL='https://github.com/dragonwell-project/dragonwell8/releases/download/dragonwell-extended-8.19.20_jdk8u412-ga/Alibaba_Dragonwell_Extended_8.19.20_aarch64_linux.tar.gz'; \
           else \
             ESUM='e804ff19d8570eb6a317b18e72d02aaede0cd4db5444eb4e3750c935afe88021'; \
             BINARY_URL='https://github.com/dragonwell-project/dragonwell8/releases/download/dragonwell-standard-8.19.20_jdk8u412-ga/Alibaba_Dragonwell_Standard_8.19.20_aarch64_linux.tar.gz'; \
           fi \
         else \
           if [ "${EDITION}" = "extended" ];then \
             ESUM='317e6f18541c03cca96edd8c49d98450b11d2f36318495544a25e4fe0daa187b'; \
             BINARY_URL='https://dragonwell.oss-cn-shanghai.aliyuncs.com/8.19.20/Alibaba_Dragonwell_Extended_8.19.20_aarch64_linux.tar.gz'; \
           else \
             ESUM='e804ff19d8570eb6a317b18e72d02aaede0cd4db5444eb4e3750c935afe88021'; \
             BINARY_URL='https://dragonwell.oss-cn-shanghai.aliyuncs.com/8.19.20/Alibaba_Dragonwell_Standard_8.19.20_aarch64_linux.tar.gz'; \
           fi \
         fi \
         ;; \
       amd64|i386:x86-64) \
         if [ "${SOURCE}" = "github" ];then \
           if [ "${EDITION}" = "extended" ];then \
             ESUM='e61ba75e5eb7922f678670ef32fe95dd8472aaa6873b8a6411f3a655c5eda4d8'; \
             BINARY_URL='https://github.com/dragonwell-project/dragonwell8/releases/download/dragonwell-extended-8.19.20_jdk8u412-ga/Alibaba_Dragonwell_Extended_8.19.20_x64_linux.tar.gz'; \
           else \
             ESUM='adce5313f61927f66c52404d76304cee1f5f994bf31a0755ba303ab359add183'; \
             BINARY_URL='https://github.com/dragonwell-project/dragonwell8/releases/download/dragonwell-standard-8.19.20_jdk8u412-ga/Alibaba_Dragonwell_Standard_8.19.20_x64_linux.tar.gz'; \
           fi \
         else \
           if [ "${EDITION}" = "extended" ];then \
             ESUM='e61ba75e5eb7922f678670ef32fe95dd8472aaa6873b8a6411f3a655c5eda4d8'; \
             BINARY_URL='https://dragonwell.oss-cn-shanghai.aliyuncs.com/8.19.20/Alibaba_Dragonwell_Extended_8.19.20_x64_linux.tar.gz'; \
           else \
             ESUM='adce5313f61927f66c52404d76304cee1f5f994bf31a0755ba303ab359add183'; \
             BINARY_URL='https://dragonwell.oss-cn-shanghai.aliyuncs.com/8.19.20/Alibaba_Dragonwell_Standard_8.19.20_x64_linux.tar.gz'; \
           fi \
         fi \
         ;; \
       *) \
         echo "Unsupported arch: ${ARCH}"; \
         exit 1; \
         ;; \
    esac; \
    curl --retry 5 -so /tmp/openjdk.tar.gz ${BINARY_URL}; \
    echo "${ESUM} */tmp/openjdk.tar.gz" | sha256sum -c -; \
    mkdir -p /opt/java/openjdk; \
    cd /opt/java/openjdk; \
    tar -xf /tmp/openjdk.tar.gz --strip-components=1; \
    export PATH="/opt/java/openjdk/bin:$PATH"; \
    bash /usr/local/bin/slim/slim-java.sh /opt/java/openjdk; \
    rm -rf /tmp/openjdk.tar.gz /usr/local/bin/slim;

ENV JAVA_HOME=/opt/java/openjdk \
    PATH="/opt/java/openjdk/bin:$PATH"

RUN echo Verifying install ... \
    && echo javac -version && javac -version \
    && echo java -version && java -version \
    && echo Complete.

RUN PREFIX=$(echo ${EDITION} | awk '{print toupper($0)}') \
    && JAVA_VERSION=$(eval echo \$${PREFIX}_JAVA_VERSION) \
    && java -version 2>&1 | grep "${JAVA_VERSION}"
